using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace SuperListEx.Sections
{
	public class CellSectionImage : CellSection
    {
        #region properties
        public Image Image
        {
            get
            {
                return (Image)HeaderColumnSection.Column.ColumnItemAccessor(Item);
            }
        }
        #endregion

        #region static
        /// <summary>
		/// Image handling CellSection registration
		/// </summary>
		/// <param name="factory"></param>
		public static void RegisterMapping( SectionFactory factory )
		{
			factory.RegisterCellSection(
				typeof( System.Drawing.Image ),
				delegate( ISectionHost host, HeaderColumnSection hcs, object item )
				{
					return new CellSectionImage( host, hcs, item );
				}
			);
        }
        #endregion

        #region constructors
        public CellSectionImage( ISectionHost host, HeaderColumnSection hcs, object item )
			: base( host, hcs, item )
		{
        }
        #endregion

        #region public functions
        public override void Layout( GraphicsSettings gs, Size maximumSize )
		{
			if( this.MinimumHeight < Image.Height )
			{
				this.MinimumHeight = Image.Height;
			}
			base.Layout( gs, maximumSize );
		}

		public override Size GetIdealSize( GraphicsSettings gs )
		{
			return new Size( Image.Width + 2, Image.Height + 2 );
		}

		public override void Paint( GraphicsSettings gs, Rectangle clipRect )
		{
			Rectangle rc = DrawRectangle;
			Image image = Image;
			gs.Graphics.DrawImage( Image, new Point( rc.Left + (rc.Width - image.Width ) / 2, rc.Top + (rc.Height - image.Height ) / 2 ) );
        }
        #endregion
    }
}
